<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
	</body>
</html>

<script>
	(function(n,vpath,varStr){
		var cache={};
		var version='1.0';
		var varStr=varStr?varStr:'cache_version';
		function xhr(file,callback){
			var x=new XMLHttpRequest();
			x.open('GET',file,false);
			x.withCredentials=true;
			x.onload=function(){
				callback(x.responseText,file)
			}
			x.send();
		}

		function parse(str,file){
			var module={exports:{}};
			var codeStr=[
				"(function(){var module={exports:{}};try{",str,"}catch(e){console.error('require "+file+"',e)};return module.exports;}())"
			].join("");
			try{		
				cache[file]=eval(codeStr);
				localStorage.setItem(file,codeStr);
			}catch(e){
				console.error('error:require '+file,e);  
			}
		}

		function r(file){
			var name=file+'?'+varStr+'='+version;
			if(!cache[name]){
				var cache2=localStorage.getItem(name);
				if(cache2){
					cache[name]=eval(cache2);
				}else{
					xhr(name,parse);
				}
			}
			return Object.create(cache[name]);
		}

		xhr(vpath,function(res){
			try{
				version=res;
				localStorage.setItem(varStr,version);
				var reg=new RegExp('\\?'+varStr+'=','g'),k;
				for(var i=0;i<localStorage.length;i++){
					k=localStorage.key(i);
					if(k.indexOf('?'+varStr+'='+version)<0 && reg.test(k)){
						localStorage.removeItem(k)
					}
				}
			}catch(err){
				console.error(vpath,err);
			}
		})
		window[n]=r;
	}('require','version.txt'));
	
	/* 
		test.js
		module.exports={
			abc:function(){
				alert('hello world');
			}
		}
	*/
	require('test.js').abc();
</script>
